package com.atguigu.tingshu.order.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author atguigu-mqx
 * @ClassName OrderReciever
 * @description: TODO
 * @date 2024年08月12日
 * @version: 1.0
 */
@Component
public class OrderReceiver {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 监听取消订单
     *
     * @param orderId
     */
    @SneakyThrows
    @RabbitListener(queues = MqConst.QUEUE_CANCEL_ORDER)
    public void orderDelayQueue(Long orderId, Message message, Channel channel) {
        try {
            //   判断
            if (null != orderId) {
                //  调用方法
                orderInfoService.orderCancel(orderId);
            }
        } catch (Exception e) {
            e.getMessage();
        }
        //  手动确认消息.
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    /**
     * 监听支付成功
     * @param orderNo
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER_PAY_SUCCESS),
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER),
            key = MqConst.ROUTING_ORDER_PAY_SUCCESS
    ))
    public void orderPaySuccess(String orderNo, Message message, Channel channel) {
        try {
            //  判断
            if (!StringUtils.isEmpty(orderNo)) {
                //  调用方法
                orderInfoService.payOrderStatus(orderNo);
            }
        } catch (Exception e) {
            e.getMessage();
        }
        //  手动确认消息.
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
